﻿using Cheems.Control.Models.Chat;
using Cheems.Interface.AI;
using Cheems.Interface.Configuration;
using Cheems.Interface.Models.UserManagement;
using Cheems.Service.Models;
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Cheems.HMI.ViewModels
{
    public class ChatWindowViewModel : BaseVM
    {
        public ChatWindowViewModel(IRegionManager regionManager, IContainerProvider containerProvider) : base(regionManager, containerProvider)
        {
            HistoryChat = new ObservableCollection<ChatHistory>();
            BaseUrl = _containerProvider.Resolve<IConfig<ChatConfig>>().GetConfig().BaseUrl;
        }

        private string _baseUrl;
        /// <summary>
        /// DeepSeek端口
        /// </summary>
        public string BaseUrl
        {
            get { return _baseUrl; }
            set 
            {
                if (_baseUrl != value)
                {
                    _containerProvider.Resolve<IConfig<ChatConfig>>().UpdateConfig(
                        it => it.BaseUrl = value
                    );
                }
                SetProperty(ref _baseUrl, value); 
            }
        }

        private string _inputMessage;
        /// <summary>
        /// 用户输入的文本
        /// </summary>
        public string InputMessage
        {
            get { return _inputMessage; }
            set { SetProperty(ref _inputMessage, value); }
        }

        private ChatHistory _currentChat;
        /// <summary>
        /// 当前聊天
        /// </summary>
        public ChatHistory CurrentChat
        {
            get { return _currentChat; }
            set { SetProperty(ref _currentChat, value); }
        }

        private ObservableCollection<ChatHistory> _historyChat;
        /// <summary>
        /// 历史聊天
        /// </summary>
        public ObservableCollection<ChatHistory> HistoryChat
        {
            get { return _historyChat; }
            set { SetProperty(ref _historyChat, value); }
        }

        private DelegateCommand<string> _sendQuestion;
        public DelegateCommand<string> SendQuestionCommand =>
            _sendQuestion ?? (_sendQuestion = new DelegateCommand<string>(ExecuteSendQuestionCommand));
        /// <summary>
        /// 发送问题
        /// </summary>
        /// <param name="parameter"></param>
        async void ExecuteSendQuestionCommand(string parameter)
        {
            HistoryChat.Add(new ChatHistory()
            {
                User = new T_User()
                {
                    Name = "用户",
                },
                Message = InputMessage,
                FlowDirection = FlowDirection.RightToLeft,
            });
            HistoryChat = HistoryChat;

            string _inputMessage = InputMessage;
            InputMessage = string.Empty;

            CurrentChat = new ChatHistory()
            {
                User = new T_User()
                {
                    Name = "DeepSeek"
                },
            };
            await Task.Run(async () =>
            {
                await foreach (var item in _containerProvider.Resolve<IChat>().GetChatGptResponse(_inputMessage))
                {
                    CurrentChat.Message += item;
                }
            });
            CurrentChat.Message = _containerProvider.Resolve<IChat>().CleanText(CurrentChat.Message);

            HistoryChat.Add(CurrentChat);
            HistoryChat = HistoryChat;
            // CurrentChat = new ChatHistory();
        }
    }
}
